package com.epalmpay.service.member.impl;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.memberGrade.MemberFeeSettingInDTO;
import com.epalmpay.dto.memberGrade.MemberFeeSettingList;
import com.epalmpay.dto.memberGrade.MemberFeeSettingOutDTO;
import com.epalmpay.entity.MemberFee;
import com.epalmpay.mapper.MemberFeeMapper;
import com.epalmpay.service.member.IMemberFeeSettingService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Properties;


@Service("memberFeeSettingService")
public class MemberFeeSettingImpl implements IMemberFeeSettingService {

    @Resource
    private MemberFeeMapper memberFeeMapper;

    @Override
    public List<MemberFee> memberFeeSettingList(MemberFee memberFee) throws ApplicationException {

        return memberFeeMapper.queryMemberFeeList( memberFee);
    }

    @Override
    public MemberFeeSettingOutDTO GetFeeSettingById(MemberFee memberFee) throws ApplicationException {
        MemberFeeSettingOutDTO memberFeeSettingOutDTO=new MemberFeeSettingOutDTO();
        MemberFee memberFee1= memberFeeMapper.selectByPrimaryKey(memberFee.getId());
        try {
            PropertyCopyUtil.copyProperties(memberFee1,memberFeeSettingOutDTO);
            memberFeeSettingOutDTO.setErrorCode("查询成功");
        }catch (Exception e){
        	e.printStackTrace();
            memberFeeSettingOutDTO.setErrorCode("数据异常");
        }

        return memberFeeSettingOutDTO;
    }

    @Override
    public MemberFeeSettingOutDTO UpdateFeeSettingById(MemberFee memberFee) throws ApplicationException {
        MemberFeeSettingOutDTO memberFeeSettingOutDTO=new MemberFeeSettingOutDTO();
        if (!Tools.isEmpty(memberFee.getId()+"")){
            MemberFee memberFee1=memberFeeMapper.selectByPrimaryKey(memberFee.getId());
            memberFee.setCreateUserName(memberFee1.getCreateUserName());
            memberFee.setCreateUserId(memberFee1.getCreateUserId());
            int status= memberFeeMapper.updateByPrimaryKey(memberFee);
            if (status>0){
                memberFeeSettingOutDTO.setErrorCode(Rescode.SUCCESS,"会员等级费率更新成功");
            }else{
                memberFeeSettingOutDTO.setErrorCode(Rescode.FAIL,"会员等级费率更新失败");
            }
            return memberFeeSettingOutDTO;
        }else{
           int status= memberFeeMapper.insert(memberFee);
           if (status>0){
               memberFeeSettingOutDTO.setErrorCode(Rescode.SUCCESS,"会员等级费率设置成功");
           }else{
               memberFeeSettingOutDTO.setErrorCode(Rescode.FAIL,"会员等级费率设置失败");
           }
           return memberFeeSettingOutDTO;
        }

    }

    @Override
    public MemberFeeSettingOutDTO SetFeeStatusById(MemberFee memberFee) throws ApplicationException {
        MemberFeeSettingOutDTO memberFeeSettingOutDTO=new MemberFeeSettingOutDTO();
        int status= memberFeeMapper.updateByPrimaryKeySelective(memberFee);
        if (status>0){
            memberFeeSettingOutDTO.setErrorCode(Rescode.SUCCESS,"会员等级费率更新成功");
        }else{
            memberFeeSettingOutDTO.setErrorCode(Rescode.FAIL,"会员等级费率更新失败");
        }
        return memberFeeSettingOutDTO;

    }

    @Override
    public List<MemberFeeSettingOutDTO> queryMemberFee(MemberFeeSettingInDTO memberFeeSettingInDTO) throws ApplicationException {

        return memberFeeMapper.queryMemberFee(memberFeeSettingInDTO);
    }

}
